package com.weetop.cfw.utils

import android.annotation.SuppressLint
import android.view.ViewGroup
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient


/**
 * WebView工具类
 */
class WebViewSettingUtils {


    companion object {

        /**
         * 初始化WebView
         */
        @SuppressLint("SetJavaScriptEnabled")
        fun initWebView(targetWebView: WebView) {

            val webSettings = targetWebView.settings
//            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
            webSettings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN

            webSettings.loadWithOverviewMode = true
            webSettings.builtInZoomControls = true
            webSettings.setSupportZoom(false)
            webSettings.javaScriptEnabled = true
            webSettings.displayZoomControls = false
            targetWebView.setWebViewClient(ArticleWebViewClient())


//            webSettings.setJavaScriptCanOpenWindowsAutomatically(true)
//            webSettings.setUseWideViewPort(true)//设置webview推荐使用的窗口
//            webSettings.setLoadWithOverviewMode(true)//设置webview加载的页面的模式
//            webSettings.setDisplayZoomControls(false)//隐藏webview缩放按钮
//            webSettings.setJavaScriptEnabled(true) // 设置支持javascript脚本
//            webSettings.setAllowFileAccess(true) // 允许访问文件
//            webSettings.setBuiltInZoomControls(false) // 设置显示缩放按钮
//            webSettings.setSupportZoom(true) // 支持缩放
////            webSettings.textSize= WebSettings.TextSize.LARGER
//            webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS)


        }

        private  class ArticleWebViewClient : WebViewClient() {
            override fun onPageFinished(view: WebView, url: String) {
                super.onPageFinished(view, url)
                //重置webview中img标签的图片大小
                imgReset(view)
            }
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                view.loadUrl(url)
                return true
            }
        }

        /**
         * 对图片进行重置大小，宽度就是手机屏幕宽度，高度根据宽度比便自动缩放
         */
        private fun imgReset( webView:WebView) {
            webView.loadUrl(
                "javascript:(function(){" +
                        "var objs = document.getElementsByTagName('img'); " +
                        "for(var i=0;i<objs.length;i++)  " +
                        "{"
                        + "var img = objs[i];   " +
                        "    img.style.maxWidth = '100%'; img.style.height = 'auto';  " +
                        "}" +
                        "})()"
            )
        }


        /**
         * 销毁WebView
         */
        fun destoryWebView(targetWebView: WebView, rootLayout: ViewGroup) {
            targetWebView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null)
            targetWebView.clearHistory()
            targetWebView.clearCache(true)
            targetWebView.clearFormData()
            rootLayout.removeView(targetWebView)
            targetWebView.destroy()
        }
    }
}